package exercicios.servlet.ex02;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/servlet/ex02")
public class TabelaParametrosServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{
		processar(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
		processar(req, res);
	}

private void processar(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Map<String,String[]> parametros = req.getParameterMap();
		PrintWriter out = res.getWriter();
		out.println("<html><head><title>Servlet - Exercicio 2</title></head><body>");
		out.println("<table border='1'><tr><th>Parametro</th><th>Valor</th></tr>");
		if(!parametros.isEmpty()){
			for (String chave: parametros.keySet()){
				//out.println("<tr><td>"+chave+"</td><td>"+Arrays.toString(parametros.get(chave))+"</td></tr>");
				out.println("<tr><td>"+chave+"</td><td>"+parametros.get(chave)[0]+"</td></tr>");
			}
		}
		out.println("</table></body></html>");
	}
}
